﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FantasyBlog.DAL;

namespace FantasyBlog.Controllers
{
    public class AdminPostController : Controller
    {
        private BlogModelContainer db = new BlogModelContainer();

        //
        // GET: /AdminPost/

        public ActionResult Index()
        {
            //List<Post> postList = db.PostSet.ToList();
            return View(db.PostSet.ToList());
        }

        //
        // GET: /AdminPost/createpost

        public ActionResult CreatePost()
        {
            return View();
        }

        //
        // GET: /AdminUser/Delete/5

        public ActionResult Delete(int id)
        {
            Post post = db.PostSet.Find(id);
            db.PostSet.Remove(post);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult EditPost(int id)
        {
            Post post = db.PostSet.Find(id);
            return View(post);
        }

        [HttpPost]
        public ActionResult EditPost(Post post)
        {
            if (ModelState.IsValid)
            {
                db.PostSet.Find(post.ID).title = post.title;
                db.PostSet.Find(post.ID).content = post.content;
                db.PostSet.Find(post.ID).excerpt = post.excerpt;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(post);
        }


        [HttpPost]
        public ActionResult CreatePost(Post post)
        {
            if (ModelState.IsValid)
            {
                //DateTime datetime = new DateTime(2011, 4, 22, 22, 42, 00);
                //post.date = datetime;
                //post.PostUser.username = "johny";
                post.date = DateTime.Now;
                post.modified_time = DateTime.Now;
                post.Category = db.CategorySet.Find(1);
                post.PostUser = db.UserSet.Find(1);
                db.PostSet.Add(post);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(post);
        }

        public ActionResult CreateCategory()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateCategory(Category category)
        {
            if (ModelState.IsValid)
            {
                db.CategorySet.Add(category);
                db.SaveChanges();
                return RedirectToAction("ScanCategory");
            }

            return View(category);
        }

        public ActionResult ScanCategory()
        {
            return View(db.CategorySet.ToList());
        }

        public ActionResult EditCategory(int id)
        {
            Category category = db.CategorySet.Find(id);
            return View(category);
        }

        [HttpPost]
        public ActionResult EditCategory(Category category)
        {
            if (ModelState.IsValid)
            {
                db.CategorySet.Find(category.ID).name = category.name;
                db.SaveChanges();
                return RedirectToAction("ScanCategory");
            }

            return View(category);
        }

    }
}
